Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Reliably quote columns/tables #1400

Merged
merged 12 commits into from
Feb 2, 2023
Merged

fix: Reliably quote columns/tables #1400

merged 12 commits into from
Feb 2, 2023

Conversation

def quote(field)
"\"#{field.to_s}\""
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in pg, is basically quote_ident. for values, should be single-quotes like quote_literal

@bf4
Copy link
Collaborator Author

bf4 commented Jan 31, 2023

Not sure what the current failure means. Work so far is an improvement, I think.. not sure what to do next

# :nocov:
else
"#{table.to_s}_#{field.to_s}"
end
end
end

def quote_table_name(table_name)
if _model_class.try(:connection)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could be _model_class&.connection

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this would be better now that we don't support ruby versions older than 2.3

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking 'what if _model_class exists but doesn't respond to connection' but then I was thinking 'this is the ar resource, it's fine', but then I was thinking, ... eh

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated to &.

@bf4 bf4 force-pushed the properly_quote branch 2 times, most recently from 7d61c8a to 8820e54 Compare February 2, 2023 15:14
@@ -494,15 +494,15 @@ def test_sorting_by_relationship_field

assert_response :success
assert json_response['data'].length > 10, 'there are enough records to show sort'
expected = Post
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lgebhardt rather than assert different rules here based on the value of adapter_sorts_nulls_last and try to reproduce the db behavior, I thought it would be sufficient to assert that the query results on the AR model give the same result as the resource.

Thoughts? I briefly tried using defining adapter_sorts_nulls_last then writing a transform in Ruby but I didn't duplicate the pg/mysql/sqlite logic and then thought, "why am I even?"

  def adapter_specific_sort_comparison(l,r, sort_type:)
    if l && r
      comparison = l <=> r
      case sort_type
      when :sort then comparison
      when :minus_sort then -comparison
      else fail ArgumentError, "Unhandled sort_type #{sort_type} in #{__callee__}"
      end
    elsif l.nil? && r.nil?
      0
    elsif l.nil?
      adapter_sorts_nulls_last ? -1 : 1
    else
      adapter_sorts_nulls_last ? 1 : -1
    end
  end

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

technically this is out of scope of quoting and is part of "make mysql" tests pass.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I extracted this into #1402


# Postgres sorts nulls last, whereas sqlite and mysql sort nulls first
if ENV['DATABASE_URL'].starts_with?('postgres')
Copy link
Collaborator Author

@bf4 bf4 Feb 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I replaced all "what adapter is this" cases I found with "what behavior do I expect"

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I extracted this into #1402

@bf4 bf4 merged commit 5c24399 into v0-11-dev Feb 2, 2023
@bf4 bf4 deleted the properly_quote branch February 2, 2023 16:22
@bf4
Copy link
Collaborator Author

bf4 commented Feb 2, 2023

@lgebhardt I decided to merge this since the target is a dev branch and this meets the goals I set re: quoting and now it can be the basis for further work

@lgebhardt
Copy link
Member

@bf4 sounds good to me. I'm not sure how to best handle the adapter based sorting differences, but we can definitely handle that in a separate pr.

lgebhardt pushed a commit that referenced this pull request Sep 19, 2023
* refactor: easily quote table/column
* refactor: extract table name when missing
* fix: Reliably quote columns/tables
* refactor: putting quoting methods together
* Handle special case of *
- tests
  * fix: hack mysql test query comparison
aytigra added a commit to aytigra/jsonapi-resources that referenced this pull request Oct 25, 2023
lgebhardt pushed a commit that referenced this pull request Apr 18, 2024
* refactor: easily quote table/column
* refactor: extract table name when missing
* fix: Reliably quote columns/tables
* refactor: putting quoting methods together
* Handle special case of *
- tests
  * fix: hack mysql test query comparison
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants